/**
 * Copyright (c) 2018-2028, HQY 胡庆一 (215218393@qq.com).
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ocm.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ocm.user.dto.AddressDTO;
import com.ocm.user.entity.Address;
import com.ocm.user.service.IAddressService;
import com.ocm.user.vo.AddressVO;
import com.ocm.user.wrapper.AddressWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import com.ocm.core.mp.support.Condition;
import com.ocm.core.mp.support.Query;
import com.ocm.core.secure.BladeUser;
import com.ocm.core.tool.api.R;
import com.ocm.core.tool.utils.Func;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ocm.core.boot.ctrl.BladeController;

import java.util.List;

/**
 *  控制器
 *
 * @author HQY
 * @since 2022-01-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("/address")
@Api(value = "", tags = "接口")
public class AddressController extends BladeController {

	private final IAddressService addressService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入address")
	public R<AddressVO> detail(Address address) {
		Address detail = addressService.getOne(Condition.getQueryWrapper(address));
		return R.data(AddressWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/u/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入address")
	public R<List<Address>> list(BladeUser bladeUser) {
		List<Address> addressList = addressService.list(new QueryWrapper<Address>().eq("user_id", bladeUser.getUserId()));
		return R.data(addressList);
	}
	/**
	 * 新增或修改
	 */
	@PostMapping("/u/submit")
	@Transactional
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入address")
	public R uSubmit(@Valid @RequestBody AddressDTO address, BladeUser  bladeUser) {
		//如果默认地址被选择 且此地址不是原来的
		if(address.getOldMainId()!=null){
			addressService.update(new UpdateWrapper<Address>().set("status",1).eq("user_id",bladeUser.getUserId()).eq("id",address.getOldMainId()));
		}

		if(address.getId()!=null){
			return R.status(addressService.update(address,new UpdateWrapper<Address>().eq("id",address.getId()).eq("user_id",bladeUser.getUserId())));
		}
		address.setUserId(bladeUser.getUserId());
		return R.status(addressService.save(address));
	}


	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入address")
	public R<IPage<AddressVO>> list(Address address, Query query) {
		IPage<Address> pages = addressService.page(Condition.getPage(query), Condition.getQueryWrapper(address));
		return R.data(AddressWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入address")
	public R<IPage<AddressVO>> page(AddressVO address, Query query) {
		IPage<AddressVO> pages = addressService.selectAddressPage(Condition.getPage(query), address);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入address")
	public R save(@Valid @RequestBody Address address) {
		return R.status(addressService.save(address));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入address")
	public R update(@Valid @RequestBody Address address) {
		return R.status(addressService.updateById(address));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入address")
	public R submit(@Valid @RequestBody Address address) {
		return R.status(addressService.saveOrUpdate(address));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(addressService.deleteLogic(Func.toLongList(ids)));
	}


}
